import {
  wxRequest
} from '../../../utils/server.js';
const _wxRequest = new wxRequest();
const app = getApp()
Page({
  data: {
    imgUrl: app.globalData.imgUrl,
    time: 0,
    mobile: wx.getStorageSync('userInfo').account
  },

  // 获取验证码
  getCode() {
    if (this.data.time > 0) return;
    _wxRequest.wxTonkenAjax({
        url: app.globalData.apiUrl + "/sys/protected/msg/sms/sendResetPwdCode",
        method: "POST"
      },
      res => {
        this.countDown(120);
        wx.showToast({
          icon: "none",
          title: "验证码发送成功"
        });
      }
    );
  },

  // 倒计时
  countDownHandler: null,
  countDown(time) {
    this.setData({
      time
    });
    this.countDownHandler = setTimeout(() => {
      if (this.data.time > 0) {
        this.countDown(this.data.time - 1);
      } else {
        this.countDownHandler = null;
      }
    }, 1000);
  },

  // 重置密码
  resetPwd: function (e) {
    var formdata = e.detail.value
    console.log(formdata);

    // if (formdata.mobile === '') {
    //   wx.showToast({
    //     icon: 'none',
    //     title: '手机号码不能为空',
    //   })
    //   return
    // }

    if (formdata.password === '' || formdata.password2 === '') {
      wx.showToast({
        icon: 'none',
        title: '密码不能为空',
      })
      return
    }

    if (formdata.password.length < 6) {
      wx.showToast({
        icon: 'none',
        title: '密码长度不能小于6位',
      })
      return
    }

    if (formdata.password != formdata.password2) {
      wx.showToast({
        icon: 'none',
        title: '新密码和确认密码不一致',
      })
      return
    }
    var that = this
    console.log(formdata);
    _wxRequest.wxTonkenAjax({
      url: app.globalData.apiUrl + '/sys/protected/msg/sms/resetPasswordBySmsCode?password=' + formdata.password + '&code=' + formdata.code,
      method: 'POST'
    }, (res) => {
      wx.showToast({
        title: '修改成功',
      })
      wx.removeStorageSync('Authorization', '');
      wx.removeStorageSync('userInfo');
      wx.removeStorageSync('authorized');

      setTimeout(() => {
        wx.navigateTo({
          url: '/pages/login/login',
        })
      }, 1000)
    })
  },








})